import http from 'k6/http';
export const host = "http://xa-dd3-hu/app1";

let enableLog = false;
let logBody = false;
let logStateCode = false;
export function EnableLog(){
    enableLog = true;
    logBody = true;
    logStateCode = true;    
}
function log(response, funcName){
    if(enableLog){
        console.log("-------" + funcName + "------------------------------------------");
        if(logStateCode){
            console.log("status:" + response.status);
            if(response.status >= 400){
                console.error(response);
                return;
            }
        }
        if(logBody){
            if(response.body){
                let body = response.body;
                body = (body.length > 100 ? (body.substring(0,100) + `  (${body.length} chars)...`): body);
                body = body.replace(/\r\n/g, '');
                console.log("body: " + body);
            }
            else{
                console.log("body is Empty");
            }
        }
    }
}

export function index(){
    const response = http.get(host,null, createDefaultHeder()); 
    log(response, "index");
}

export function GetRSAPublicKey(){
    const res = http.post(host + '/Account/GetRSAPublicKey',null, createDefaultHeder());
    log(res, "GetRSAPublicKey");
    return res;
}

export function login(userName = "Administrator"){
    const response = http.post(encodeURI(`${host}/Account/Login`),
        JSON.stringify( {
            "userName": userName,
            "password": "123456",
            "rememberMe": false,
            "deviceInfo": "25538D1E-3B9A-4E10-9FA2-3D850A47A8A6,PDF Viewer,Chrome PDF Viewer,Chromium PDF Viewer,Microsoft Edge PDF Viewer,WebKit built-in PDF,1415"
        }), createDefaultHeder());

        log(response, "Login");
        return response; 
}

export function GetMetadata(pageName = "FGC_登录"){
    const response = http.post(
        encodeURI(`${host}/Home/GetMetadata`),
        {
          pageName: `PathName:/Forguncy/${pageName}}`,
          token: "",
          isMobile: "false",
        },createDefaultHeder()
      );
      log(response, "GetMetadata");
      return response;
}

export function GetMetadata2(pageName = "FGC_登录"){
    pageName = encodeURI(pageName);
    const response = http.get(`${host}/Home/GetMetadata2?pageName=${pageName}`, null, createDefaultHeder());
    log(response, "GetMetadata2");
    return response; 
}

export function GetMetadataGlobleConfig(){
    const response = http.get(`${host}/Home/GetMetadata2?pageName=90AC3BB5-87DF-4E25-B6C3-A78CC93DF159&isMobile=false&v2=-600239608-598159596-598149576-598409572`,null, createDefaultHeder());
    log(response, "GetMetadataGlobleConfig");
    return response; 
}

function createDefaultHeder(modityHeader = null){
    const header = {
        Accept: "*/*",
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "zh-CN,zh;q=0.9",
        //"Cache-Control": "no-cache",
        Connection: "keep-alive",
        "Content-Type": "application/json",
        //Pragma: "no-cache",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-origin",
        "X-Requested-With": "XMLHttpRequest",
        "sec-ch-ua-platform": '"Windows"',
      };
    if(modityHeader){
        for (const key in modityHeader){
            header[key] = modityHeader[key];
        }
    }
    return {
        headers:header
    };
}